package com.oocl.jpa.dao.impl;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.oocl.jpa.pojo.Customer;


@Repository(value="customerDao")
public class CustomerDaoImpl implements com.oocl.jpa.dao.CustomerDao {

	@Resource
	private EntityManager entityManager; 
			
	@Override
	public Customer addCustomer(Customer c) {
		try {
			entityManager.getTransaction().begin();
			entityManager.persist(c);
			entityManager.getTransaction().commit();
			/*entityManager.close();*/
			return c;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Customer findCustomer(String cname, String psd) {
		/*String hql = "from customer where cname = ? and password= ?";*/
		Query query = entityManager.createQuery("from Customer where cname='"+cname+"' and password='"+psd+"'");
		if(query.getResultList().size() == 0){
			return null;
		}else{
			Customer cust = (Customer) query.getSingleResult();
			System.out.println("jpa 查询出来的customer信息为 : "+cust);
			return cust;
		}		
	}
}



























